<template>
	<view class="search">
		<view class="search-icon">
			<view class="search-view">
				<image :src="imgUrl + '/image/icon-search.png'" class="search-img"></image>
				<input v-show="isReach" ref="inputRef" class="input-search" type="text" maxlength="30"
					v-model="inputValue" :placeholder="placeholder" placeholder-style="color:#333333"
					:focus="searchFocus" @input="searchChange" />
				<input v-show="!isReach" class="input-search" type="text" :placeholder="placeholder"
					placeholder-style="color:#8E8F97" @focus="focus" />
			</view>
			<uni-icons v-if="isReach" type="closeempty" size="18" color="#8e8f97" @tap="inputClear"></uni-icons>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			placeholder: {
				type: String,
				default: '请输入'
			},
		},
		data() {
			return {
				imgUrl: '',
				isReach: false,
				inputValue: '',
				searchFocus: false,
			}
		},
		created() {
			// 初始化imgUrl
			if (this.$mConfig && this.$mConfig.imageUrl) {
				this.imgUrl = this.$mConfig.imageUrl;
			}
		},
		methods: {
			inputClear(e) {
				console.log('shanchu')
				this.isReach = false;
				this.inputValue = '';
				this.searchFocus = false;
			},
			searchChange(e) {
				let timer = null;
				if (timer) {
					clearTimeout(timer);
				}
				setTimeout(() => {
					this.$emit('searchChange', this.inputValue)
				}, 250)
			},
			focus() {
				this.isReach = true;
				setTimeout(() => {
					this.searchFocus = true;
				}, 300)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search {
		padding: 19rpx 30rpx 46rpx 30rpx;
		width: 100%;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		color: #222;
		box-sizing: border-box;

		.search-icon {
			flex: 1;
			background: #F3F5FA;
			border-radius: 16rpx;
			padding: 23rpx 14rpx 23rpx 26rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.search-view {
				display: flex;
				flex-direction: row;
				align-items: center;
				width: 100%;

				.search-img {
					width: 32rpx;
					height: 32rpx;
					margin-right: 14rpx;
				}

				.input-search {
					width: 100%;
					font-size: 28rpx;
					color: #333333;
				}
			}
		}

		.r-cancel {
			padding-right: 30rpx;
			padding: 0;
			font-size: 30rpx;
			background: transparent;
			color: #ffffff;
		}
	}
</style>